﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO.Compression;

namespace server
{
    public partial class Form1 : Form
    {
        private string mnue;
        private string members;
        static ArrayList clients = new ArrayList();
        public string root_path;
        Build mybuild = new Build();
        TreeNode my_rootree;
        Socket_Link mylink = new Socket_Link();
        Thread voicemeethread;
        private ArrayList Names = new ArrayList();

        //获取本机IP地址，并转换
        public static IPAddress GetServerIP()
        {
            IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
            return ieh.AddressList[0];
        }

        public Form1()
        {
            InitializeComponent();
            skinEngine1.SkinFile = "DeepCyan.ssk";
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            //读取文件内容

            StreamReader sr = new StreamReader("root_path.txt", System.Text.Encoding.GetEncoding("gb2312"));
            this.root_path = sr.ReadToEnd();
            sr.Close();
        }

        private void 文档目录设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetPath open = new SetPath();
            open.ShowDialog();
            if (open.path!=null)
                 root_path =open.path;
        }
        private void update()
        {
            tree_show_file.Nodes.Clear();
            tree_show_file.ShowLines = true;
            tree_show_file.ImageList = imageList1;
            my_rootree = new TreeNode("项目汇总");//根目录
            my_rootree.Tag = my_rootree;
            my_rootree.ImageIndex = 0;
            my_rootree.SelectedImageIndex = 0;
            tree_show_file.Nodes.Add(my_rootree);
            //递归现实子目录
            showtreeview(root_path, my_rootree, 0);
            tree_show_file.ExpandAll();
        }
        private void 现实现有项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            update();
        }

        //递归建立子目录
        private void showtreeview(string fillpath, TreeNode nowtreenode, int depth)
        {
            //显示内容
            DirectoryInfo CurDir = new DirectoryInfo(fillpath);
            //得到当前目录下的所有文件夹
            DirectoryInfo[] dirs = CurDir.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                TreeNode btree = new TreeNode(dir.Name);
                btree.Tag = dir.FullName;
                if (depth == 0)
                {
                    btree.ImageIndex = 1;
                    btree.SelectedImageIndex = 1;
                }
                else if (depth == 1)
                {
                    btree.ImageIndex = 2;
                    btree.SelectedImageIndex = 2;
                }
                nowtreenode.Nodes.Add(btree);
                showtreeview(dir.FullName, btree, 1);
            }
        }
        /***************************************************************************************/
        //
        //
        //          罗列文件函数
        //
        //
        /****************************************************************************************/
        private void FillFilesView(string FullName)
        {
            listView1.Clear();
            listView1.View = View.Details;
            listView1.Columns.Add("文件名称", listView1.Width / 3, HorizontalAlignment.Left);
            listView1.Columns.Add("文件类型", listView1.Width / 6, HorizontalAlignment.Left);
            listView1.Columns.Add("文件大小", listView1.Width / 6, HorizontalAlignment.Left);
            listView1.Columns.Add("创建时间", listView1.Width / 3, HorizontalAlignment.Left);
            //显示内容
            DirectoryInfo CurDir = new DirectoryInfo(FullName);
            //得到当前目录下的所有文件夹
            DirectoryInfo[] dirs = CurDir.GetDirectories();
            //得到当前目录下的所有文件
            FileInfo[] files = CurDir.GetFiles();

            //显示文件夹
            foreach (DirectoryInfo dir in dirs)
            {
                ListViewItem listview = new ListViewItem();
                listview.Text = dir.Name;
                listview.ImageIndex = 1;
                listview.Tag = dir.FullName;
                listview.SubItems.Add("文件夹");
                listview.SubItems.Add("");
                listview.SubItems.Add(dir.LastAccessTime.ToString());

                listView1.Items.Add(listview);
            }

            foreach (FileInfo file in files)
            {
                float a = 0;
                float b = file.Length;
                ListViewItem listview = new ListViewItem();
                listview.Text = file.Name;
                listview.ImageIndex = 3;
                listview.Tag = file.FullName;
                listview.SubItems.Add("文件");
                if (file.Length < 1024)
                    listview.SubItems.Add(file.Length.ToString() + "B");
                else if (file.Length > 1024 && file.Length < 1024 * 1024)
                {
                    a = (b / 1024);
                    listview.SubItems.Add(a.ToString("0.00") + "KB");
                }
                else if (file.Length > 1024 * 1024)
                {
                    a = b / (1024 * 1024);
                    listview.SubItems.Add(a.ToString("0.00") + "MB");
                }
                listview.SubItems.Add(file.LastAccessTime.ToString());
                listView1.Items.Add(listview);
            }
        }

        private void tree_show_file_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (tree_show_file.SelectedNode != null && tree_show_file.SelectedNode.ImageIndex == 2)
                {
                    FillFilesView(tree_show_file.SelectedNode.Tag.ToString());
                }
            }
        }

        private void 新建项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProject open = new NewProject(root_path);
            open.ShowDialog();
            if(open.DialogResult==DialogResult.OK)
            {
                update();
            }
        }

        private void tree_show_file_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (tree_show_file.SelectedNode != null && tree_show_file.SelectedNode.ImageIndex == 0)
                {
                    contextMenuStrip1.Items[0].Visible = true;
                    contextMenuStrip1.Items[1].Visible = false;
                    contextMenuStrip1.Items[2].Visible = false;
                    contextMenuStrip1.Items[3].Visible = false;
                }
                else if (tree_show_file.SelectedNode != null && tree_show_file.SelectedNode.ImageIndex == 1) 
                {
                    contextMenuStrip1.Items[0].Visible = false;
                    contextMenuStrip1.Items[1].Visible = true;
                    contextMenuStrip1.Items[2].Visible = true;
                    contextMenuStrip1.Items[3].Visible = false;
                }
                else if (tree_show_file.SelectedNode != null && tree_show_file.SelectedNode.ImageIndex == 2)
                {
                    contextMenuStrip1.Items[0].Visible = false;
                    contextMenuStrip1.Items[1].Visible = false;
                    contextMenuStrip1.Items[2].Visible = false;
                    contextMenuStrip1.Items[3].Visible = true;
                }
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                try
                {
                    System.Diagnostics.Process.Start(listView1.SelectedItems[0].Tag.ToString());
                }
                catch
                {
                    MessageBox.Show("不支持该文件格式，无法打开文件");
                }
            }
        }   

        private void 添加新项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewProject open = new NewProject(root_path);
            open.ShowDialog();
            if (open.DialogResult==DialogResult.OK)
            {
                update();
            }
        }

        private void 添加项目组成员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewMember open = new NewMember(tree_show_file.SelectedNode.Tag.ToString(),root_path,tree_show_file.SelectedNode.Text);
            open.ShowDialog();
            if (open.DialogResult==DialogResult.OK)
            {
                update();
            }
        }

        private void 人员信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            person_mng open = new person_mng();
            open.ShowDialog();
        }

        private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mnue = "";
            members = "";
           //获得项目和成员列表
            mybuild.setrootpath = root_path;
            mnue = mybuild.Build_menu();
            members = mybuild.Build_member();
            //连接服务器
            mylink.getForm = this;
            mylink.getmember = members;
            mylink.getmenu = mnue;
            mylink.getbuild = mybuild;
            mylink.getrootpath = root_path;
            mylink.Bindport();        
        }
              
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            mylink.close_link();
            if (voicemeethread != null) 
            {
                voicemeethread.Abort();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DirectoryInfo CurDir = new DirectoryInfo(this.root_path);
            if (CurDir.Exists)
            {
                启动ToolStripMenuItem_Click(sender, e);
            }
            else 
            {
                MessageBox.Show("请重新设置根目录");
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            mylink.close_link();
            toolStripStatusLabel3.Image = global::server.Properties.Resources.led_red;
            toolStripStatusLabel2.Image = global::server.Properties.Resources.led_off;
            toolStripButton2.Enabled = false;
            toolStripButton1.Enabled = true;
            toolStripStatusLabel1.Text = " 已关闭服务器.....";   
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            现实现有项目ToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            人员信息ToolStripMenuItem_Click(sender, e);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.notifyIcon1.Visible = false;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

        private void 显示主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = false;
        }

        private void 退出服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //构造文件所在文件夹路径
        private string selectpath(string path) 
        {
            string[] get = path.Split(new char[] { '\\'});
            string wantpath = "";
            for (int i = 0; i < get.Length-1; i++) 
            {
                wantpath =wantpath+ get[i]+"\\";
            }
            return wantpath;
        }
        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                openpath.Enabled = true;
                openfile.Enabled = true;
            }
            else
            {
                openpath.Enabled = false;
                openfile.Enabled = false;
            }
        }

        private void openpath_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                try
                {
                    System.Diagnostics.Process.Start(selectpath(listView1.SelectedItems[0].Tag.ToString()));
                }
                catch
                {
                    MessageBox.Show("路径错误");
                }
            }
        }

        private void openfile_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                try
                {
                    System.Diagnostics.Process.Start(listView1.SelectedItems[0].Tag.ToString());
                }
                catch
                {
                    MessageBox.Show("不支持该文件格式，无法打开文件");
                }
            }
        }

        private void 开启语音通讯ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            voicemeethread = new Thread(new ThreadStart(voicemeeting));
            voicemeethread.Start();
        }

        private void voicemeeting() 
        {
            voicemeeting open = new voicemeeting(GetServerIP());
            open.getatline = mylink;
            open.ShowDialog();  
        }

        private void 删除该项目组成员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确认要删除该项目组成员的所有信息", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                string parentpath;
                parentpath = tree_show_file.SelectedNode.Parent.Tag + "\\Job.XML";
                XML_Job Job = new XML_Job(parentpath);
                if (Job.Delete(tree_show_file.SelectedNode.Text))
                {
                    ProjectToUserDB PToDB = new ProjectToUserDB(root_path + "\\Project.xml", parentpath);
                    if (PToDB.DeleteOneUser(tree_show_file.SelectedNode.Text, tree_show_file.SelectedNode.Parent.Text))
                    {
                        DeleteFolder Delete = new DeleteFolder();
                        string deletepath = tree_show_file.SelectedNode.Parent.Tag + "\\" + tree_show_file.SelectedNode.Text;
                        Delete.Deletefolder(deletepath);
                        update();
                    }
                }
            }
        }

        private void 删除项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确认要删除该项目的所有信息", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                string parentpath;
                parentpath = root_path + "\\" + tree_show_file.SelectedNode.Text + "\\Job.xml";
                XML_Job Job = new XML_Job(parentpath);
                Names = Job.GetName();
                for (int i = 0; i < Names.Count; i++)
                {
                    ProjectToUserDB PToDB = new ProjectToUserDB(root_path + "\\Project.xml", parentpath);
                    PToDB.DeleteOneUser(Names[i].ToString(), tree_show_file.SelectedNode.Text);
                }
                ProjectToUserDB PjToDB = new ProjectToUserDB(root_path + "\\Project.xml", parentpath);
                if (PjToDB.DeleteOneProject(tree_show_file.SelectedNode.Text))
                {
                    XML_Project Project = new XML_Project(root_path + "\\Project.xml");
                    if (Project.Delete(tree_show_file.SelectedNode.Text))
                    {
                        DeleteFolder Delete = new DeleteFolder();
                        string deletepath = root_path + "\\" + tree_show_file.SelectedNode.Text;
                        if (Delete.Deletefolder(deletepath))
                        {
                            update();
                        }
                    }
                }
            }

        }
       
       
    }
}